/*
** handle_monitor.c for zappy/serv in /home/Smaxo/Travail/zappy-2015-2014s-renard_g/serv-src
** 
** Made by maximilien addari
** Login   <addari_m@epitech.net>
** 
** Started on  Tue Jul 10 16:10:46 2012 maximilien addari
** Last update Sun Jul 15 20:01:47 2012 maximilien addari
*/

#include	"server.h"

static void	send_player(t_list *list, t_client *graph, t_game *game)
{
  t_client	*client;
  t_item	*tmp;

  tmp = list->head;
  while (tmp != NULL)
    {
      client = (struct s_client *)tmp->content;
      if (client->gamer != NULL)
	serv_pnw(client, game, graph);
      tmp = tmp->next;
    }
}

static void	send_egg(t_client *graphic, t_game *game)
{
  t_event	*event;
  t_item	*cur;
  t_fork	*egg;

  if (game->event->len > 0)
    {
      cur = game->event->head;
      while (cur != NULL)
	{
	  event = (struct s_event *)cur->content;
	  if (event->type == FORK)
	    {
	      egg = (struct s_fork *)event->data;
	      graphic->send->concat(graphic->send, "enw ");
	      graphic->send->concat_value(graphic->send, egg->id);
	      graphic->send->concat(graphic->send, " ");
	      graphic->send->concat_value(graphic->send, egg->client->sock);
	      graphic->send->concat(graphic->send, " ");
	      graphic->send->concat_value(graphic->send, egg->coord.x);
	      graphic->send->concat(graphic->send, " ");
	      graphic->send->concat_value(graphic->send, egg->coord.y);
	      graphic->send->concat(graphic->send, "\n");
	    }
	}
    }
}

char		handle_connect_mnt(t_game *game, t_list *list, t_event *event)
{
  (void)event;
  if (game->sleep == 0 || game->sleep > 10000)
  {
    cmd_monitor("msz", game->graphic, game);
    cmd_monitor("sgt", game->graphic, game);
    cmd_monitor("mct", game->graphic, game);
    cmd_monitor("tna", game->graphic, game);
    send_player(list, game->graphic, game);
    send_egg(game->graphic, game);
    return (0);
  }
  return (1);
}
